package com.settlement.system.controller;

import com.settlement.system.common.exception.ServiceException;
import com.settlement.system.model.entity.common.BasePager;
import com.settlement.system.model.entity.common.SinglePage;
import com.settlement.system.model.entity.common.TblReceiveRefundAccount;
import com.settlement.system.model.entity.common.TblReceiveRefundShop;
import com.settlement.system.reply.ObjectDataResponse;
import com.settlement.system.service.IBaseService;
import com.settlement.system.service.ITblReceiveRefundShopService;
import com.settlement.system.service.ITblRrChannelService;
import com.settlement.system.common.util.common.FasUtil;
import com.alibaba.excel.context.AnalysisContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 收退款店铺 前端控制器
 * @author Ximi
 * @since 2020-06-12
 */
@Slf4j
@Api(tags = "基础配置-店铺",description = "基础配置-店铺")
@RestController
@RequestMapping("/receive_refund/shop")
public class TblReceiveRefundShopController extends MergeApiExcelController<TblReceiveRefundShop, Map<String,Object>> {

    @Resource
    private ITblReceiveRefundShopService tblReceiveRefundShopService;

    @Resource
    private ITblRrChannelService tblRrChannelService;

    @Override
    public IBaseService init() {
        return tblReceiveRefundShopService;
    }

    @Override
    public Class<?> getModelClass() {
        return TblReceiveRefundShop.class;
    }

    @Override
    public String verifyImportObject(TblReceiveRefundShop tblReceiveRefundShop, AnalysisContext analysisContext) {
        if(StringUtils.isEmpty(tblReceiveRefundShop.getThreeChannelName())){
            return "三级来源名称不能为空";
        }
        if(!StringUtils.isEmpty(tblReceiveRefundShop.getChannelName())){
            String channelNo = tblRrChannelService.getNoByName(tblReceiveRefundShop.getChannelName());
            if(StringUtils.isEmpty(channelNo)){
                return "渠道名称不正确";
            }
            tblReceiveRefundShop.setChannelNo(channelNo);
        }
        try{
            TblReceiveRefundShop shop = tblReceiveRefundShopService.selectExistShop(tblReceiveRefundShop);
            if(shop!=null){
                return "数据已存在";
            }
        } catch (ServiceException e) {
            log.error(e.getMessage(),e);
            throw new RuntimeException(e);
        }

        return super.verifyImportObject(tblReceiveRefundShop, analysisContext);
    }

    @Override
    protected ObjectDataResponse insert(@RequestBody TblReceiveRefundShop model, HttpServletRequest request) throws ServiceException {
        TblReceiveRefundShop shop = tblReceiveRefundShopService.selectExistShop(model);
        if(shop!=null){
            return new ObjectDataResponse(500, "数据已经存在");
        }
        return super.insert(model, request);
    }

    @Override
    protected ObjectDataResponse update(@RequestBody TblReceiveRefundShop model, HttpServletRequest request) throws ServiceException {
        TblReceiveRefundShop shop = tblReceiveRefundShopService.selectExistShop(model);
        if(shop!=null && !model.getId().equals(shop.getId())){
            return new ObjectDataResponse(500, "数据已经存在");
        }
        return super.update(model, request);
    }

    @Override
    protected void setParamsModel(Map<String, Object> model) {
        FasUtil.formatInQueryCondition(model,"shopNo","enterAccountBody");
        super.setParamsModel(model);
    }

    @ApiOperation("NC店铺")
    @RequestMapping("getNcShop")
    public ObjectDataResponse getNcShop(@RequestBody BasePager<Map<String, Object>> model) {
        Map<String, Object> result = new HashMap<>(16);
        try {
            int count = tblReceiveRefundShopService.getNcShopCount(model.getModel());
            List<TblReceiveRefundShop> rows = tblReceiveRefundShopService.getNcShop(new SinglePage(model), model.getModel(),
                    model.getOrderByField(), model.getOrderBy());
            result.put("rows", rows);
            result.put("total", count);
        } catch (Exception ex) {
            log.error("获取列表出错", ex);
            return new ObjectDataResponse(500, "获取列表失败，请联系管理员！");
        }
        return new ObjectDataResponse<Map>(result, 200, "获取列表成功");
    }


    @Override
    protected String validateAllDate(List<TblReceiveRefundShop> datas) {
        List<String> list = new ArrayList<>();
        for(TblReceiveRefundShop data : datas){
            String toString = data.toUniqueString();
            if(list.contains(toString)){
                return "有两行数据重复";
            }else{
                list.add(toString);
            }
        }

        return super.validateAllDate(datas);
    }
}

